package org.darcy.youchat.activity.message;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import org.darcy.youchat.R;
import org.darcy.youchat.activity.BaseActivity;
import org.darcy.youchat.databinding.ActivityRedPacketBinding;
import org.darcy.youchat.utils.UIUtils;

/**
 * 3.红包
 */
public class RedPacketActivity extends BaseActivity {

    private ActivityRedPacketBinding binding;
    private final String titleName = "红包";

    @Override
    public void initView() {
        binding = ActivityRedPacketBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.mToolbar.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(titleName);
        binding.mToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);
        getSupportActionBar().setSubtitle("微信安全支付");
        binding.mToolbar.toolbar.setBackgroundColor(UIUtils.getColor(R.color.red1));
    }

    @Override
    public void initListener() {
        binding.etMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    binding.btnOk.setEnabled(false);
                    binding.tvMoney.setText("￥0.00");
                } else {
                    binding.btnOk.setEnabled(true);
                    String result = String.format("%.2f", Double.valueOf(s.toString()));
                    binding.tvMoney.setText("￥" + result);
                    Double money = Double.valueOf(result);
                    if (money > 200) {
                        binding.tvTip.setVisibility(View.VISIBLE);
                        binding.tvMoneyLeft.setTextColor(UIUtils.getColor(R.color.red5));
                        binding.etMoney.setTextColor(UIUtils.getColor(R.color.red5));
                        binding.tvMoneyRight.setTextColor(UIUtils.getColor(R.color.red5));
                        binding.btnOk.setEnabled(false);
                    } else {
                        binding.tvTip.setVisibility(View.GONE);
                        binding.tvMoneyLeft.setTextColor(UIUtils.getColor(R.color.black0));
                        binding.etMoney.setTextColor(UIUtils.getColor(R.color.black0));
                        binding.tvMoneyRight.setTextColor(UIUtils.getColor(R.color.black0));
                        binding.btnOk.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        binding.etMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                binding.tvHint.setVisibility(TextUtils.isEmpty(s) ? View.VISIBLE : View.GONE);
                if (s.length() > 25) {
                    binding.etMessage.setText(s.subSequence(0, 25));
                    binding.etMessage.setSelection(25);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.menu_one_icon, menu);
        menu.getItem(0).setIcon(R.mipmap.ic_question);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case R.id.itemOne:
                UIUtils.showToast("有问题？");
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
